package com.dc.service.impl;

import com.dc.dao.ShopcarDao;
import com.dc.em.ErrorMessageEnum;
import com.dc.model.OShopcar;
import com.dc.model.Shopcar;
import com.dc.service.ShopcarService;
import com.dc.vo.BaseModel;
import com.dc.vo.BusinessException;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShopcarServiceImpl implements ShopcarService {
    @Autowired
    ShopcarDao shopcarDao;
    public BaseModel selectPageShopcarById(Integer id, Integer curr_page, Integer page_size, BaseModel baseModel) throws Exception {
        PageHelper.startPage(curr_page,page_size);
        List<OShopcar> shopcars = shopcarDao.selectPageShopcar(id);
        PageInfo pageInfo = new PageInfo(shopcars,5);
        baseModel.setResultCode(0);
        baseModel.setMessage("查询成功");
        baseModel.setData(pageInfo);
        return baseModel;
    }

    public BaseModel addShopcar(Shopcar shopcar, BaseModel baseModel) throws Exception {
        int result = shopcarDao.insertShopcar(shopcar);
        if(result <= 0){
            throw new BusinessException(ErrorMessageEnum.ADD_SHOPCAR_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("加入购物车成功");
        return baseModel;
    }

    public BaseModel deletedShopcar(String ids, BaseModel baseModel) throws Exception {
        String[] idArry = ids.split(",");
        int result = shopcarDao.deletedShopcar(idArry);
        if(result <=0 ){
            throw new BusinessException(ErrorMessageEnum.DELETED_SHOPCAR_ERROR);
        }
        baseModel.setResultCode(0);
        baseModel.setMessage("删除"+result+"条记录成功");
        return baseModel;
    }
}
